package com.starblink.utils.app.animator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.frame.base.BaseViewModel
import com.starblink.frame.base.activity.BaseTVMActivity
import com.starblink.frame.base.adapter.MutableAdapter
import com.starblink.frame.base.adapter.SimpleGapItemDecor
import com.starblink.utils.app.R
import com.starblink.utils.app.adapter.CellFadeInAnimator
import com.starblink.utils.app.databinding.ActivityFadeInAnimatorBinding
import com.starblink.utils.app.databinding.CellFadeInItemBinding
import com.starblink.utils.app.vm.FadeInAnimatorVM
import com.starblink.utils.ext.function.dp2px
import com.starblink.utils.view.animators.FadeInAnimator

class FadeInAnimatorActivity : BaseTVMActivity<ActivityFadeInAnimatorBinding,FadeInAnimatorVM>() {
    private val fadeInanimatorAdapter by lazy {
        MutableAdapter(viewModel.fadeInAnimatorData).apply {
            addVhDelegate {
                CellFadeInAnimator(CellFadeInItemBinding.inflate(layoutInflater,it,false))
            }
        }
    }
    override fun initData() {
    }

    override fun initView() {
        val layoutManager= GridLayoutManager(this,3)
        viewBinding.rvData.layoutManager = layoutManager
        viewBinding.rvData.adapter = fadeInanimatorAdapter
        viewBinding.rvData.addItemDecoration(
            SimpleGapItemDecor(
                horGap = 10.dp2px(),
                verGap = 10.dp2px()
            )
        )
        //使用动画
        val itemAnimator = FadeInAnimator()
        itemAnimator.addDuration = 2000
        itemAnimator.removeDuration = 4000
        viewBinding.rvData.itemAnimator = itemAnimator

        viewBinding.btnAdd.setOnClickListener {
            //添加
            viewModel.addFadeInAnimatorData()
        }
        viewBinding.btnSub.setOnClickListener {
            //减少
            viewModel.subFadeInAnimatorData()
        }
    }

    override fun initObservable() {
        viewModel.fadeInAnimatorLiveData.observe(this){
            if(it) {
                //Inserted 方法才会触发动画
                fadeInanimatorAdapter.notifyItemRangeInserted(0, 1)
            }else{
                fadeInanimatorAdapter.notifyItemRangeRemoved(0, 1)
            }
        }
    }

    override fun initViewBinding(): ActivityFadeInAnimatorBinding {
        return ActivityFadeInAnimatorBinding.inflate(layoutInflater)
    }
}